package com.itita.ww2.handler.requesthandler.game;

import java.sql.SQLException;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.itita.ww2.core.MessageHandler;
import com.itita.ww2.core.R;
import com.itita.ww2.core.WW2Exception;
import com.itita.ww2.enums.WW2ExceptionCode;
import com.itita.ww2.game.user.UserService;
import com.itita.ww2.handler.requesthandler.army.ArmyProduceComplete;
import com.itita.ww2.handler.requesthandler.building.BaseBDUpgradeComplete;
import com.itita.ww2.model.enums.ResourceType;
import com.itita.ww2.model.user.DevTime.DevTimeType;
import com.itita.ww2.model.user.UserProfile;
import com.itita.ww2.utils.DevKeyUtil;
import com.itita.ww2.utils.TimeUtil;
import com.itita.ww2.utils.handler.IRequestOper;

/**
 * 加速   时间
 * @author NorthLan
 *
 */
public class Expedite implements IRequestOper {

	@Override
	public boolean execute(String requestCMD, User user, ISFSObject params) throws WW2Exception, SQLException {
		UserProfile userProfile = UserService.getInstance().getUserProfile(user);
		String key = params.getUtfString("devKey");
		String code = DevKeyUtil.splitKey(key, 1);
		// 
		ISFSObject backObj = new SFSObject();
		backObj.putInt(R.DATA_KEY_COUNTER, params.getInt(R.DATA_KEY_COUNTER));
		// 
		DevTimeType typeEnum = DevTimeType.keyValueOf(key);
		if (typeEnum == null) {
			throw new WW2Exception(WW2ExceptionCode.INVALID_OPT, backObj,
					"DevTimeType Exception " + DevKeyUtil.splitKey(key, 0).toUpperCase());
		}
		// 当前单位不处于升级状态是不行的
		if (!userProfile.getTimeManager().isDevTimeExists(typeEnum, code)) {
			MessageHandler.getInstance().sendMsg(requestCMD, backObj, user, WW2ExceptionCode.INVALID_OPT, false);
			return false;
		}

		// 金条是否足够消耗
		int needGold = userProfile.getTimeManager().calculateTimeGold(typeEnum, code);
		if (!userProfile.getResManager().isResEnoughSingle(ResourceType.GOLD, needGold)) {
			MessageHandler.getInstance().sendMsg(requestCMD, backObj, user, WW2ExceptionCode.USER_RES_NOT_ENOUGH,
					false);
			return false;
		}

		// 备份当前时间,失败后恢复
		// 将升级时间修改为现在
		long timeBackup = userProfile.getTimeManager().updateDevTime(typeEnum, code, TimeUtil.getUtcNowMillis());
		userProfile.getTimeManager().update();

		// 升级
		IRequestOper execute = null;
		switch (typeEnum) {
		case UPGRADE:
			if (code.contains("BA")) {
				execute = new BaseBDUpgradeComplete();
			}
			//			else if (code.contains("S")) {
			//				 execute = new ScienceUpgradeComplete();
			//			}
			//						else if (code.contains("Map")) {
			//							execute = new MapDefUpgradeComplete();
			//						} else if (code.contains("U")) {
			//							execute = new UnitUpgradeComplete();
			//						}
			break;
		case PRODUCE:
			// 生产
			execute = new ArmyProduceComplete();
			break;
		default:
			// DO NOTHING...
			break;
		}
		if (execute != null) {
			if (!execute.execute(requestCMD, user, params)) {
				// 升级失败,恢复时间
				userProfile.getTimeManager().updateDevTime(typeEnum, code, timeBackup);
			} else {
				// 消耗资源
				userProfile.getResManager().operRes(ResourceType.GOLD, -needGold);
				// 推资源消息
				userProfile.getResManager().sync();
			}
		} else {
			userProfile.getTimeManager().updateDevTime(typeEnum, code, timeBackup);
		}

		//发送结果至用户
		MessageHandler.getInstance().sendMsg(requestCMD, backObj, user, null, true);
		return true;
	}

}
